package cn.wolfcode.web.controller;

import cn.wolfcode.web.domain.Employee;
import cn.wolfcode.web.jsonResult.JsonResult;
import cn.wolfcode.web.service.ILoginService;
import cn.wolfcode.web.util.UserContext;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;

@Controller
public class LoginController {
    @Autowired
    private ILoginService loginService;

    @RequestMapping("/login")
    @ResponseBody
    public JsonResult login(String username, String password) {

        try {
//            把用户上传的密码和账号存在令牌里面
            UsernamePasswordToken token=new UsernamePasswordToken(username,password);
//            主体通过令牌,调用登录方法
            SecurityUtils.getSubject().login(token);
            Employee employee = loginService.selectUsernameAndPassword(username, password);

            return new JsonResult();
        } catch (UnknownAccountException e) {
            return new JsonResult(false, "账号不存在");     }
        catch (IncorrectCredentialsException e) {
            return new JsonResult(false, "密码错误");     }
        catch (Exception e) {
            e.printStackTrace();
            return new JsonResult(false, "登录异常，请联系管理员");
        }
        }

}
